﻿function RefreshNavPanel() {
	var val = dateSelect.val();
	
	var param = {
		NavPnl: {
			NavLefBon:{	OnClick:"dateSelect.prev()" },
			NavRigBon:{	OnClick:"dateSelect.next()" },
			NavTitle:{ Text: dateSelect.text() }
		}
	};
	
	if( !Date.isToday(val) ){
		param.NavPnl.NavBackBon = { OnClick:"dateSelect.today()" };
	}
	
	window.WebApi && WebApi.LoadWebStyle( JSON.stringify(param) );
}

// 日期选择
var dateSelect = {
	// 周几
	weeks : Lang["weekdays"].split(","),
    
    // 前一天
    prev : function() {
        var val = dateSelect._val;
        val.setDate( val.getDate() - 1 );
        dateSelect.val(val, 1);
    },
    // 后一天
    next : function() {
		var val = dateSelect._val;
        val.setDate( val.getDate() + 1 );
        dateSelect.val(val, 0);
    },
	// 当天
	today : function() {
		dateSelect.val( new Date() );
	},
	// 是否当天
	isToday : function() {
		return Date.isToday(dateSelect._val);
	},
    // 设置当前日期
    val : function(val, f) {
        if (val) {
            dateSelect._val = val;
            $.hash( "date", val.getTime() );
            dateSelect.refresh(f);
        } else {
            return dateSelect._val;
        }
    },
    // 格式化的日期
	text : function(){
		var v = dateSelect._val;
		return v.toString(Lang["dateformat"]) + " " + dateSelect.weeks[v.getDay()];
	},
	
    // 刷新显示
	refresh : function(f) {
		var val = this._val; output = val.toString(Lang["dateformat"]) + " " + this.weeks[val.getDay()];
		
		RefreshNavPanel();
		
		$("#currentDay").html( output );
		this.onRefresh && this.onRefresh.call(this, f);
	},
	
	// 初始化
	init : function(val) {
		var t = this, d = new Date();

		!isNaN(val) && d.setTime(val)
				
		$("#prevDay").on("mousedown, touchstart", function() {
			t.prev();
		});
		$("#nextDay").on("mousedown, touchstart", function() {
			t.next();
		});
		
		t.val(d);
	}
};